package com.test;
import java.io.IOException;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class CommodityMapper extends Mapper<LongWritable, Text, Text, Commodity>{

    protected void map(LongWritable key1, Text value1,Context context)throws IOException, InterruptedException{
        String data = value1.toString();
        //分词
        String [] words = data.split(",");
        //获取年份
        String year = words[2].substring(0,4);
        //创建商品对象
        Commodity e = new Commodity();
        //设置商品的属性
        //产品ID
        e.setProd_id(Integer.parseInt(words[0]));
        //客户ID
        e.setCust_id(Integer.parseInt(words[1]));
        //日期
        e.setData(words[2]);
        //渠道ID
        e.setChannel_id(Integer.parseInt(words[3]));
        //促销ID
        e.setPromo_id(Integer.parseInt(words[4]));
        //销售的数量（件）
        e.setQuantity_sold(Integer.parseInt(words[5]));
        //销售的总额（元）
        e.setAmount_sold(Float.valueOf(words[6]));
        //输出: k2 日期 v2 销售笔数
        context.write(new Text(year),e);
    }
}
